package EightQueens;

import java.util.Random;

/**
 * 
 * @author Sebastian Brodehl, Dennis Meyer
 *
 */

public class Individual {
	
	public int geneLength = 8;
	public Byte[] gene = new Byte[geneLength];
	
	// Is value calc one time, we can save it.
	private int fitness = 0;
	private boolean fitnessAlreadyClac = false;
	
	
	public static Individual generateRandomIndividual() {
		Random r = new Random();
		
		Individual tempIndividual = new Individual();
		for (int i = 0; i < tempIndividual.geneLength; i++) {
			tempIndividual.gene[i] = (byte) (r.nextInt(8-0));
		}
		
		return tempIndividual;
	}


	public int getFitness() {
		if (fitnessAlreadyClac)
			return this.fitness;
		
		this.fitness = CalculateFitness.getFitness(this);
		this.fitnessAlreadyClac = true;
		return this.fitness;
	}
	
	public static Byte getRandomGene()
	{
		Random r = new Random();
		return (byte) (r.nextInt(8-0));
	}
	
	@Override
	public String toString()
	{
		StringBuilder geneStringBuilder = new StringBuilder();
		for (int i = 0; i < this.geneLength; i++) {
			geneStringBuilder.append(this.gene[i]);
			
			if (i < this.geneLength - 1)
				geneStringBuilder.append(" ");
		}
		
		return geneStringBuilder.toString();
	}
}
